const PEDDING = "pedding";
const FULFILLED = "fulfilled";
const REJECTED = "rejected";

class MyPromise {
    constructor(executor) {
        try {
            executor(this.resolve, this.reject);
        } catch (err) {
            this.reject(err);
        }
    }

    state = PEDDING;
    value = null;
    reason = null;

    resolve = (value) => {
        if (this.state === PEDDING) {
            this.state = FULFILLED;
            this.value = value;
        }
    };

    reject = (reason) => {
        if (this.state === PEDDING) {
            this.state = REJECTED;
            this.reason = reason;
        }
    };

    then(onFulfilled, onRejected) {
        if (this.state === FULFILLED) {
            onFulfilled(this.value);
        }
        if (this.state === REJECTED) {
            onRejected(this.reason);
        }
    }
}
